package ding.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import ding.entity.Book;
import ding.service.BookService;
import ding.service.Impl.BookServiceImpl;
import ding.utils.DateUtil;
import ding.utils.FileImageUtil;
import ding.utils.Page;
import ding.utils.UUIDUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/book.action")
@MultipartConfig
public class BookServlet extends HttpServlet {
    BookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr")==null?"list":req.getParameter("opr");
        switch (opr){
            case "list":
                //展示书籍列表
                //获取前台参数
                list(req, resp);
                return;
            case "edit":
                edit(req, resp);
                return;
            case "detail":
                detail(req, resp);
                return;
            case "del":
                del(req,resp);
                return;
        }
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String book = req.getParameter("id");
        int del = bookService.delBook(Integer.parseInt(book));
        String jsonString = JSON.toJSONString(del);
        resp.getWriter().println(jsonString);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        Book book = bookService.detail(id);

        String json = JSON.toJSONString(book, SerializerFeature.WriteNullStringAsEmpty);

        resp.getWriter().print(json);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Book book = new Book();
        String bookId = req.getParameter("bookId").equals("")?null:req.getParameter("bookId");
        book.setBookName(req.getParameter("bookName"));
        book.setAuthor(req.getParameter("author"));
        book.setPublisher(req.getParameter("publisher"));
        Date publisherDate = DateUtil.dateFormat(req.getParameter("publisherDate"));
        book.setPublisherDate(publisherDate);
        book.setStockQuantity(Integer.parseInt(req.getParameter("stockQuantity")));
        book.setDescription(req.getParameter("description"));
        //书籍封面
        Part part = req.getPart("coverImage");
        //上传到服务器的路径
        String uploadUrl = FileImageUtil.fileUploadUrl(req,"/upload",part);
        book.setCoverImage(uploadUrl);
        book.setBookType(1);
        boolean b;
        if (bookId == null){
            book.setIsbn(UUIDUtil.uuid());
            b = bookService.saveBook(book);
        }else {
            book.setBookId(Integer.parseInt(bookId));
            b = bookService.updateBook(book);
        }
        Map<String, String> map = new HashMap<>();
        if (b){
            map.put("code","200");
        }else {
            map.put("code","500");
        }

        String jsonString = JSON.toJSONString(map);
        resp.getWriter().println(jsonString);
    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bookName = req.getParameter("bookName")==""?null: req.getParameter("bookName");
        String author = req.getParameter("author")==""?null: req.getParameter("author");

       int pageNum = req.getParameter("pageNum")==""?1:Integer.parseInt(req.getParameter("pageNum"));

        Page<Book> books = bookService.selectAll(bookName, author,pageNum);

        String jsonString = JSON.toJSONString(books,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteDateUseDateFormat
        );
        resp.getWriter().println(jsonString);
    }
}
